﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YLServerInterface.FunctionalClass;
using YLPublicService.TransferModel;
using YLServiceCLient.YLAdminClient;

namespace YLServerInterface.Forms
{
    /// <summary>
    /// Interaction logic for frmTemplateBuilder2.xaml
    /// </summary>
    public partial class frmTemplateBuilder2 : Window
    {
        public frmTemplateBuilder2()
        {
            InitializeComponent();
            initialTemplateItems();
        }
        private void initialTemplateItems()
        {
            lstExistedTemplate.Items.Clear();
            foreach (var templateItem in Caches.TemplateItems)
            {
                lstExistedTemplate.Items.Add(templateItem.mTemplateDetial.mName);
            }
            Caches.RefleshTemplateItems();
        }
        private TemplateItem templateItem
        {
            get
            {
                if (lstExistedTemplate.SelectedValue == null)
                {
                    return null;
                }
                var item = from t in Caches.TemplateItems
                           where t.mTemplateDetial.mName == lstExistedTemplate.SelectedValue.ToString()
                           select t;
                if (item.Count() > 0)
                {
                    return item.First();
                }
                else return null;
            }
        }

        private void lstExistedTemplate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tempalteShower.DataContext = templateItem;
            tempalteShower.BindingDataContext();
        }

        private void btnNewTemplate_Click(object sender, RoutedEventArgs e)
        {
            frmAddTemplate frmAddT = new frmAddTemplate();
            frmAddT.ShowDialog();
            initialTemplateItems();
            //TemplateItem t = new TemplateItem();
            //t.mID = "";
            //t.mEnable = "false";
            //t.mTemplateDetial.mName = "";
            //t.mTemplateDetial.mPrice = "0";
            //tempalteShower.DataContext = t;
            //tempalteShower.BindingDataContext();
        }

        private void templateMenuItemDelete_Click(object sender, RoutedEventArgs e)
        {
            var client = AdminServiceClient.GetServiceClient();
            client.DeleteTemplate(Convert.ToInt32(templateItem.mID));
        }

    }
}
